package cwinsor.com.leaf_raking;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;

import org.w3c.dom.NamedNodeMap;



import org.w3c.dom.NodeList;


/**
 * @author cwinsor
 *
 */
public class CustomerServices {

	// the list of rakers
	ArrayList<Customer> customerList;

	// debug
	private int debugSection = 0;
	String dM = this.getClass().getSimpleName() + "----";
	String dF = this.getClass().getSimpleName() + "---- ERROR ---- ";

	/**
	 * constructor
	 */
	public CustomerServices() {

		customerList = new ArrayList<Customer>();

	}

	/* 
	 * add a raker to the list
	 * 
	 */
	public void addRaker(Customer customer) {

		customerList.add(customer);
	}

	/*
	 * read in data from the csv file and internalize into a list
	 */
	public void readCsvFile() {

		System.out.println(dM + "reading csv file...");

		String csvInFile = "C:/Users/cwinsor/Documents/me/_BHRA_2013/leaf raking/schedule_files/2013_Leaf_Raking.csv";

		BufferedReader br = null;
		String line = "";
		String cvsSplitBy = ",";

		try {

			br = new BufferedReader(new FileReader(csvInFile));

			while ((line = br.readLine()) != null) {

				// read data into a string array, use comma as separator
				String[] theFields = line.split(cvsSplitBy);

				// convert to an ArrayList object
				ArrayList<String> theFieldsArrayObject = new ArrayList<String>();
				for (String s : theFields) {
					theFieldsArrayObject.add(s);
				}

				// skip the first line of input (header)
				if (theFieldsArrayObject.get(0).toString().equals("Start time")) {
					; // do nothing
				} else {				
					// debug print
					if (debugSection  == 1) {
						System.out.println(dM + "here  " + theFieldsArrayObject.size());
						System.out.println(dM + "here0  " + theFieldsArrayObject.get(0).toString());
						System.out.println(dM + "here1  " + theFieldsArrayObject.get(1).toString());
						System.out.println(dM + "here2  " + theFieldsArrayObject.get(2).toString());
						System.out.println(dM + "here3  " + theFieldsArrayObject.get(3).toString());
						System.out.println(dM + "here4  " + theFieldsArrayObject.get(4).toString());
						System.out.println(dM + "here5  " + theFieldsArrayObject.get(5).toString());
						System.out.println(dM + "here6  " + theFieldsArrayObject.get(6).toString());
						System.out.println(dM + "here7  " + theFieldsArrayObject.get(7).toString());
						System.out.println(dM + "here8  " + theFieldsArrayObject.get(8).toString());
						System.out.println(dM + "here9  " + theFieldsArrayObject.get(9).toString());
						System.out.println(dM + "here10 " + theFieldsArrayObject.get(10).toString());
						System.out.println(dM + "here11 " + theFieldsArrayObject.get(11).toString());
						System.out.println(dM + "here12 " + theFieldsArrayObject.get(12).toString());
						System.out.println(dM + "here13 " + theFieldsArrayObject.get(13).toString());
						System.out.println(dM + "here14 " + theFieldsArrayObject.get(14).toString());


						System.out.println(dM
								+ " " + theFieldsArrayObject.get(6).toString());
					}

					// add a customer
					Customer customer = new Customer();
					customer.addData(theFieldsArrayObject);
					customerList.add(customer);
				}
			}
			System.out.println(dM + "there are " + customerList.size() + " customers");

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		System.out.println(dM + "reading csv file DONE");

		// check
		this.check();
	}


	/*
	 * print the data to screen
	 */
	public void check() {
		System.out.println(dM + "checking... ");

		for (RakerColumns rakerColumns: RakerColumns.DAYS) {
			for (Team team: Team.TEAMS_NO_U) {
				for (Shift shift: Shift.SHIFTS) {

					// check that the number of customers == 1
					CustomerServices c = getCustomersDayShiftTeam(rakerColumns,shift,team);
					if (/*r.count() != 1*/true) {
						System.out.println(dM
								+ rakerColumns.name()
								+ " " + shift.name()
								+ " " + team.name()
								+ " has "
								+ c.count() + " customers");
					}
				}
			}
			System.out.println(dM);
		}

		for (Customer c: customerList) {
			if (c.getTeam() == Team.TEAM_UNASSIGNED) {
				System.out.println(dM
						+ "Customer "
						+ c.getName()
						+ "is NOT ASSIGNED TO A TEAM !!!");
			}
		}

		System.out.println(dM + "checking done ");
	}


	/*
	 * print the data to screen
	 */
	public void print() {
		Iterator<Customer> iterator = customerList.iterator();
		while (iterator.hasNext()) {
			Customer c = iterator.next();
			System.out.println(dM + c.toStringCommaDelimitedAllFields());
		}
	}

	/*
	 * return a String of all contents in commma-separated-value format
	 */
	public String toCsvString() {
		String in = "";

		// debug print
		if (debugSection == 3) {
			System.out.println(dM + " customerList.size = " + customerList.size());
		}
		Iterator<Customer> iterator = customerList.iterator();
		while (iterator.hasNext()) {
			Customer c = iterator.next();

			in = in
					//+ c.getEmail().toString()
				
					+ "," + "customer"
					// + "," + c.getDay().name()
					+ "," + c.getStartTime().name()
					// + "," + c.getShift().name()
					// + "," + c.getTeam().name()
					+ "," + c.getName().toString()
					+ ","
					+ ",(h)" + c.getPhone().toString()
				
					+ "," + c.getAddress().toString()
					+ "," + c.getSizeOfArea().toString()
					+ "," + c.getNotes().toString();
			in = in + "\n";
		}
		// debug print
		if (debugSection == 3) {
			System.out.println(dM + "---- toCsvString...  done");
		}
		return in;
	}


	/*
	 * return a RakerList of rakers given day, team, shift
	 */
	public CustomerServices getCustomersDayShiftTeam(RakerColumns rakerColumns, Shift shift, Team team) {
		CustomerServices theList = new CustomerServices();

		// for each entry in the list
		for (Customer c : customerList) {
			if (c.getDay().equals(rakerColumns) && c.getShift().equals(shift) && c.getTeam().equals(team)) {
				theList.addCustomer(c); 
			}
		}
		return theList;	
	}

	/* 
	 * add a customer to the list
	 * 
	 */
	private void addCustomer(Customer c) {
		customerList.add(c);
	}

	/*
	 * count
	 * returns the number of rakers in the internal raker list
	 */
	public int count() {
		return customerList.size();
	}

}